﻿define(function (require) {
    var router = require('plugins/router');
    var datacontext = require('modules/dataservice');
    var app = require('durandal/app');
    var dialog = require('plugins/dialog');

    var vm = {
        order: ko.observable(),
        orderLine: ko.observable(),
        items: ko.observableArray(),
        success: ko.observable(false),
        close: function () {
            var self = this;
            var response = { success: self.success(), orderLine: self.orderLine };
            if (!self.success())
                self.orderLine().entityAspect.rejectChanges();
            dialog.close(self, response);
        },
        activate: function () {
            var self = this;
            var tempOrderLine = datacontext.createOrderLine({ Quantity: 1 });
            tempOrderLine.isSelected = ko.observable();
            self.orderLine(tempOrderLine);
            return datacontext.getItems().then(function (data) {
                self.items(data.results);
            });
        },
        selectItem: function (item) {
            vm.orderLine().ItemId(item.Id());
            vm.success(true);
            vm.close();
        },
        deactivate: function () {
            var self = this;
            self.success(false);
        }
    };
    return vm;
});